<?php
class Admin_AdminManufacturersController extends  Zendvn_Controller_Action{
	private $_dirUpload;
	public function init(){
		parent::init();		
		$this->_templateObj->setPath(TEMPLATE_PATH . '/admin/white');		
		$this->_templateObj->load();	
		
		$ss = new Zend_Session_Namespace($this->_ssNamespace);
//		$ss->unsetAll();
		if(empty($ss->col)){
			$ss->col = 'id';
			$ss->order = 'ASC';
		}
		$ss->setExpirationSeconds(600);
		$this->_arrParam['arrFilter'] = $ss->getIterator();
		
		$pageNumber = $this->_request->getParam('page',1);
		$this->_paning['page'] = $pageNumber;
		$this->_paning['rowCount'] = $this->_configObj->getSite('list_limit');		
		$this->_paning['pageRange'] = 3;				
		$this->_arrParam['panigator'] = $this->_paning;
		
		
		$this->view->arrParam = $this->_arrParam;
		$options = array("slide","banner","quảng cáo");
		$this->view->options = $options;
	}

	public function indexAction(){
		$this->view->Title = $this->view->translate('Slider Manager') . ' : '
							 .  $this->view->translate('Manufacturers') . ' : '
							 .  $this->view->translate('List');
		$this->view->headTitle($this->view->Title,true);
		$tblCat = new Zendvn_Model_Table_Manufacturers();
		$this->view->Items = $tblCat->listItem($this->_arrParam);			
	}

	public function addAction(){
		$this->view->Title = $this->view->translate('Slider Manager') . ' : '
							 .  $this->view->translate('Manufacturers') . ' : '
							 .  $this->view->translate('Add');
		$this->view->headTitle($this->view->Title,true);
		$tblCat = new Zendvn_Model_Table_Manufacturers();

		if($this->_request->isPost()){			
			$validator = new Admin_Form_ValidateManufacturers($this->_arrParam);			
			if($validator->isVaild()){
				$this->view->errors = $validator->getMessageErrors();
				$this->view->Item 	= $validator->getData();				
			}else{
				$this->_helper->flashMessenger->addMessage(array('success'=>$this->view->translate('The update was successful')));
				$data =  $validator->getData();		
				$tblCat->saveItem($data,array('task'=>'admin-add'));
				if($this->_arrParam['type'] == 'back'){
					$this->_redirect($this->_mainAction);
				}else{
					$this->_redirect($this->_currentController . '/' . $this->_arrParam['action']);
				}
			}			
		}
		$this->_helper->viewRenderer('form');
	}

	public function editAction(){
		$this->view->Title = $this->view->translate('Slider Manager') . ' : '
							 .  $this->view->translate('Manufacturers') . ' : '
							 .  $this->view->translate('Edit');
		$this->view->headTitle($this->view->Title,true);
		if($this->_request->isPost()){			
			$validator = new Admin_Form_ValidateManufacturers($this->_arrParam);			
			if($validator->isVaild()){
				$this->view->errors = $validator->getMessageErrors();
				$this->view->Item 	= $validator->getData();				
			}else{
				$this->_helper->flashMessenger->addMessage(array('success'=>$this->view->translate('The update was successful')));
				$data =  $validator->getData();	
				$tblCat = new Zendvn_Model_Table_Manufacturers();
				$tblCat->saveItem($data,array('task'=>'admin-edit'));
				if($this->_arrParam['type'] == 'back'){
					$this->_redirect($this->_mainAction);
				}else{
					$this->_redirect($this->_currentController 
									 . '/' . $this->_arrParam['action'] 
									 . '/id/' . $this->_arrParam['id']);
				}
			}			
		}
		$tblCat = new Zendvn_Model_Table_Manufacturers();
		$this->view->Item = $tblCat->getItem($this->_arrParam);
		$this->_helper->viewRenderer('form');
	}

	public function deleteAction(){
		$tblCat = new Zendvn_Model_Table_Manufacturers();
		if($this->_arrParam['type'] == 'multi'){
			$tblCat->deleteItem($this->_arrParam, array('task'=>'multi'));
		}else{
			$tblCat->deleteItem($this->_arrParam);
		}
		$this->_helper->flashMessenger->addMessage(array('success'=>$this->view->translate('The delete was successful')));
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
	}

	public function infoAction(){
		$this->view->Title = $this->view->translate('Slider Manager') . ' : '
							 .  $this->view->translate('Manufacturers') . ' : '
							 .  $this->view->translate('Info');
		$this->view->headTitle($this->view->Title,true);
		$tblCat = new Zendvn_Model_Table_Manufacturers();
		$this->view->Item = $tblCat->getItem($this->_arrParam,array('task'=>'info'));				
	}

	public function statusAction(){
		$tblCat = new Zendvn_Model_Table_Manufacturers();
		if($this->_arrParam['type'] == 'multi'){
			$tblCat->statusItem($this->_arrParam,array('task'=>'multi'));
		}else{
			$tblCat->statusItem($this->_arrParam);
		}
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
	}

	public function sortAction(){		
		if($this->_request->isPost()){
			$tblCat = new Zendvn_Model_Table_Manufacturers();
			$tblCat->sortItem($this->_arrParam);
			$this->_redirect($this->_mainAction);
		}
		$this->_helper->viewRenderer->setNoRender();
	}

	public function filterAction(){
		$ss = new Zend_Session_Namespace($this->_ssNamespace);
		if($this->_arrParam['type'] == 'search'){
			$ss->keyword = $this->_arrParam['keyword'];
			$ss->searchField = $this->_arrParam['search-field'];
		}
		if($this->_arrParam['type'] == 'clear'){
			$ss->keyword = '';
			$ss->searchField = '';
		}

		if($this->_arrParam['type'] == 'filter'){
			$ss->status = $this->_arrParam['status'];
		}
		if($this->_arrParam['type'] == 'sort'){
			$ss->col = $this->_arrParam['col'];
			$ss->order = $this->_arrParam['order'];
		}
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
	}

	public function removePicAction(){
		$config = new Zendvn_Controller_Config(array('module'=>$this->_arrParam['module'],
													 	  'fileConfig'=>'manufacturers.ini'));
		$upload = new Zendvn_File_Upload();
		$tblCat = new Zendvn_Model_Table_Manufacturers();		
		$pictureConfig = $config->getConfig('item');
		$upload->remove($pictureConfig['uploadDir'],$this->_arrParam['file']);
		$tblCat->update(array('picture'=>''), 'id = ' . $this->_arrParam['id']);
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout()->disableLayout();
	}	

	

}